package air.tools.deadline
{
	import air.data.file.FileOperate;
	
	import flash.desktop.NativeApplication;
	import flash.events.Event;
	import flash.net.registerClassAlias;
	import flash.utils.ByteArray;
	
	import work.tools.deadline.IQualification;
	import work.tools.deadline.QualificationEndDate;
	import work.tools.deadline.QualificationGroups;
	import work.tools.deadline.QualificationOpenTotal;
	import work.tools.deadline.QualificationPassDay;

	public class DeadlineAIR
	{
		private var qualificationGroups:QualificationGroups;
		private var keyByte:ByteArray;
		private var separator:String="<>+-<>";
		
		private var pass:String="0";
		private var path:String="ApplicationConfig.dat";
		private var _isFirst:Boolean=false;
		public function DeadlineAIR(date:Date=null)
		{
			
			NativeApplication.nativeApplication.addEventListener(Event.EXITING,onExitingHandler);
			//注册 work.tools.deadline.QualificationGroups 用于强转
			flash.net.registerClassAlias("work.tools.deadline.QualificationGroups",QualificationGroups);
			/*registerClassAlias("work.tools.deadline.IQualification",IQualification);
			registerClassAlias("work.tools.deadline.QualificationEndDate",QualificationEndDate);
			registerClassAlias("work.tools.deadline.QualificationPassDay",QualificationPassDay);
			registerClassAlias("work.tools.deadline.QualificationOpenTotal",QualificationOpenTotal);*/
			var data:*=FileOperate.readObject(path);
			if(data==null)
			{
				qualificationGroups=new QualificationGroups(date);
				qualificationGroups.openCount=1;
				_isFirst=true;
			}else
			{
				/*trace(data);
				var byte:ByteArray=new ByteArray();
				byte.writeUTFBytes(String(data));
				trace(byte);
				byte.position=0;
				trace(byte.length);
				var newByte:ByteArray=XXTEA.decrypt(byte,keyByte);				
				trace(newByte);
				newByte.position=0;
				var s:String=newByte.readUTFBytes(newByte.bytesAvailable);
				trace(s);
				byte.clear();
				newByte.clear();
				
				var arr:Array=s.split(separator);
				pass=arr[0];
				qualificationGroups=QualificationGroups.StringToQualificationGroups(arr[1]);
				qualificationGroups.openCount++;*/
				qualificationGroups=QualificationGroups(data);
				qualificationGroups.openCount++;
				_isFirst=false;
			}
		}
		
		public function get isFirst():Boolean
		{
			return _isFirst;
		}

		private function saveConfig():void
		{
			/*var s:String=pass+separator+qualificationGroups.toString();
			var byte:ByteArray=new ByteArray();
			byte.writeUTFBytes(s);
			trace(s);
			var newByte:ByteArray=XXTEA.encrypt(byte,keyByte);
			newByte.position=0;
			s=newByte.readUTFBytes(newByte.bytesAvailable);
			trace("de:"+XXTEA.decrypt(newByte,keyByte));
			trace(newByte.length);
			trace(s);
			var s:String;
			var byte:ByteArray=new ByteArray();
			byte.writeObject(qualificationGroups);
			byte.position=0;
			s=byte.readUTF();
			trace(s);*/
			FileOperate.writeObject(path,qualificationGroups);
		}
		/**
		 * 起始时间,只能设置一次
		 * @return 
		 * 
		 */
		public function get startDate():Date
		{
			return qualificationGroups.startDate;
		}
		
		public function set startDate(value:Date):void
		{
			if(qualificationGroups.startDate != value)
			{
				qualificationGroups.startDate=value;
			}
		}
		/**
		 * 是否过期 
		 * @return 
		 * 
		 */
		public function isPass():Boolean
		{
			if(pass=="1")
			{
				//saveConfig();
				return true;
			}
			var bool:Boolean=qualificationGroups.isPass();
			if(bool)
			{
				pass="1";
				saveConfig();
			}
			return bool;
		}
		/**
		 * 获得剩余的期限天数 
		 * @return 如果返回正数，表示还有几天过期，如果返回负数，表示已经过期了几天
		 * 
		 */
		public function getSurplus():Number
		{
			if(pass=="1")
			{
				return -10000;
			}
			var num:Number=qualificationGroups.getSurplus();
			if(num<0)
			{
				pass="1";
				saveConfig();
			}
			return num;
		}
		/**
		 * 添加一个期限条件 
		 * @param qualification 条件
		 * @see IQualification
		 */
		public function addQualification(qualification:IQualification):void
		{
			qualificationGroups.addQulification(qualification);
		}
		
		/**
		 * 删除一个期限条件 
		 * @param className 期限条件类名
		 * @return 如果有返回这个期限条件，否则返回null
		 * 
		 */
		public function removeQualification(className:String):IQualification
		{
			return qualificationGroups.removeQulification(className);
		}
		/**
		 * 删除所有期限条件 
		 * 
		 */
		public function removeAllQualification():void
		{
			qualificationGroups.removeAllQulification();
		}
		
		private function onExitingHandler(e:Event):void
		{
			saveConfig();
		}
	}
}